package com.element.security.exception;

import com.common.core.exception.ExceptionFactory;
import com.common.core.results.VoidResult;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;

import java.io.IOException;
import java.util.Map;

/**
 * @auther zhangwj
 * @date 2021/3/24 下午8:42
 */
public class AuthExceptionSerializer extends StdSerializer<AuthException> {

    protected AuthExceptionSerializer() {
        super(AuthException.class);
    }

    @Override
    public void serialize(AuthException value, JsonGenerator jsonGenerator, SerializerProvider provider) throws IOException {
        VoidResult voidResult = value.getResult();
        if (null == voidResult) {
            throw ExceptionFactory.sysException("voidResult is null");
        }
        jsonGenerator.writeStartObject();
        jsonGenerator.writeObject(voidResult);
        if (value.getAdditionalInformation() != null) {
            for (Map.Entry<String, String> entry : value.getAdditionalInformation().entrySet()) {
                String key = entry.getKey();
                String add = entry.getValue();
                jsonGenerator.writeStringField(key, add);
            }
        }
        jsonGenerator.writeEndObject();
    }
}
